import 'package:flutter/material.dart';

import 'package:flutter_map/flutter_map.dart';
import 'package:latlong2/latlong.dart';
import '../../helper/Helper.dart';
import '../../helper/channelHelper.dart';
import '../../pages/gmap/gotoMap.dart';

import 'gmapHelper.dart';
import 'gpsHelper.dart';

class MyMap extends StatefulWidget {
  @override
  MyMap({
    LatLng? location,
    double? zoom,
    List<LayerOptions>? layers,
    LatLng? point,
    bool? reLocation,
  }) {
    this.Layers = layers ?? <LayerOptions>[];
    Location = (location ?? null)!;
    this.Zoom = zoom ?? 16.0;
    this.TPoint = point ?? null;
    this.ReLocation = reLocation ?? false;
  }

  late List<LayerOptions> Layers;
  late LatLng? Location;
  late double Zoom;
  late LatLng? TPoint;
  late bool ReLocation;

  @override
  MyMapState createState() => new MyMapState();
}

class MyMapState extends State<MyMap> {
  late LatLng location;
  late List<LayerOptions> netLayers;
  late List<LayerOptions> allLayers;

  @override
  initState() {
    location = widget.Location!;

    super.initState();

    if (widget.ReLocation) {
      if (location == null) location = new LatLng(31.0277, 103.5961);
      getLocation();
    }
    _getNetLayers();
  }

  Future getLocation() async {
    await Helper.CheckLocationPersmission();
    ChannelHelper.LocationChannel.receiveBroadcastStream().listen(onData);
  }

  void onData(dynamic data) {
    var st = data.toString();
    print(st);
    var loc1 = GMapHelper.getLocation(st);
    var loc2 = GpsHelper.transform(loc1.latitude, loc1.longitude);

    setState(() {
      location.latitude = loc2.latitude;
      location.longitude = loc2.longitude;
    });
  }

  _getNetLayers() {
    //var x1 = CacheHelper.GetLayerList();
    //netLayers = LayerHelper.layerList(x1);
    allLayers = _loadLayer();
  }

  List<LayerOptions> _loadLayer() {
    var _layers = GMapHelper.NewLayerList();
    _layers.add(GMapHelper.TileGooleMap());

    if (location != null) _layers.add(GMapHelper.MyLocation(location));

    if (widget.TPoint != null) {
      _layers.add(GMapHelper.MyMarker2(widget.TPoint!, "", () => {}));
    }
    //_layers.addAll(GMapHelper.DituLayerList());
    for (var item in widget.Layers) {
      _layers.add(item);
    }

/*
    if (netLayers != null) {
      for (var item in netLayers) {
        _layers.add(item);
      }
    }
*/
    return _layers;
  }

  Widget getFloatingButton(BuildContext context) {
    if (widget.ReLocation) {
      return FloatingActionButton(
          child: Icon(Icons.local_activity),
          onPressed: () async {
            var p1 = await ChannelHelper.getLocation2();
            print(p1);
            setState(() {
              location = p1;
            });
          });
    }

    if (widget.TPoint != null) {
      return FloatingActionButton(
        child: Icon(Icons.golf_course),
        onPressed: () {
          GoToMap.gotoTheMap(widget.TPoint!.longitude, widget.TPoint!.latitude);
          print('222');
        },
      );
    }

    return new Container();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        floatingActionButton: getFloatingButton(context),
        body: Container(
          child: new Center(
            child: new FlutterMap(
              options: new MapOptions(
                center: widget.TPoint != null ? widget.TPoint : location,
                zoom: widget.Zoom,
              ),
              layers: allLayers,
            ),
          ),
        ));
  }

  @override
  void dispose() {
    //注意这里关闭

    super.dispose();
  }
}
